home *** CD-ROM | disk | FTP | other *** search
- /*
-
- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
-
- ALPHABET - Simplistic representation of a set of characters,
- for use with the NString class. Could be replaced by a more general
- alphabet representation, being a subclass of a general set-class.
-
- Version 1.0 - beta
-
- Copyright © 12th November, 1994 by Joël "Obiwan" François
-
- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
-
- */
-
- #ifndef _ALPHABET_H_
- #define _ALPHABET_H_
-
- #define BUFSIZE 32 /* 32 bytes == 256 characters / 8 bit */
- #define ELT_HIGH 255 /* the number of the highest possible element */
- #define ELT_LOW 0 /* the number of the lowest possible element */
-
- class NString;
-
- class Alphabet
- {
- private:
-
- unsigned char buffer[BUFSIZE]; // 256 bits to hold the presence state of each letter
-
- public:
-
- // Note: All functions returning an Alphabet&, like "+=" etc., return a reference to *this
- // to allow function call chaining, e.g.: b = (a += 'J') - c;
- // The example line first adds the letter 'J' to the alphabet a, then stores the difference
- // between the modified alphabet a and the alphabet c in the variable b.
-
- /*-------- Constructors & Destructor --------*/
-
- inline Alphabet (void); // Alphabet a;
- // Create an empty alphabet.
-
- Alphabet (const char *source); // Alphabet a("YNQ");
- // Create an alphabet from a string: only the string's letters
- // will be members of the alphabet.
-
- Alphabet (const char source); // Alphabet a('D');
- // Create an alphabet from a letter
-
- Alphabet (const Alphabet& source); // Alphabet a(b);
- // Create an alphabet from another alphabet.
-
- Alphabet (const NString& source); // Alphabet a(s);
- // Create an alphabet from an NString.
-
- Alphabet (const char start, const char end); // Alphabet a('A', 'Z');
- // Create an alphabet that contains all the letters
- // in the range from "start" to "end". If the given
- // range is empty (i.e. the starting letter's ASCII value
- // is greater than the ending letter's value), the constructed
- // alphabet will be empty. If start==end, the alphabet will
- // contain nothing but the specified letter.
-
- ~Alphabet (void) {}
-
-
- /*-------- General operations that affect the whole alphabet --------*/
-
- Alphabet& clear (void); // a.clear();
- // Clear the alphabet.
-
-
- /*-------- Assignment --------*/
-
- Alphabet& operator= (const char *source); // a = "Obiwan";
- Alphabet& operator= (const NString& source); // a = s;
- // Reinitialize the alphabet with the letters of the given string.
-
- Alphabet& operator= (const char source); // a = 'C';
- // Reinitialize the alphabet with a single letter.
-
- Alphabet& operator= (const Alphabet& source); // a = b;
- // Copy the source alphabet into this alphabet.
-
-
- /*-------- Adding/Removing letters to/from the alphabet --------*/
-
- Alphabet& operator+= (const char element); // a += 'q';
- // Add the given element to the alphabet
-
- inline Alphabet operator+ (const char element) const; // a = b + 'q';
- // Return a new alphabet which is constructed by
- // adding the given element to this alphabet
-
- Alphabet& operator-= (const char element); // a -= 'q';
- // Remove the given element from the alphabet
-
- inline Alphabet operator- (const char element) const; // a = b - 'q';
- // Return a new alphabet which is constructed by
- // removing the given element from this alphabet
-
-
- Alphabet& operator+= (const char *source); // a += "Addendum";
- Alphabet& operator+= (const NString& source); // a += s;
- // Add the given string's letters to the alphabet
-
- inline Alphabet operator+ (const char *source) const; // a = b + "Addendum";
- inline Alphabet operator+ (const NString& source) const; // a = b + s;
- // Return a new alphabet which is constructed by
- // adding the given string's letters to this alphabet
-
- Alphabet& operator-= (const char *source); // a -= "2 B Removed";
- Alphabet& operator-= (const NString& source); // a -= s;
- // Remove the given string's letters from the alphabet
-
- inline Alphabet operator- (const char *source) const; // a = b - "2 B Removed";
- // Return a new alphabet which is constructed by
- // removing the given string's letters from this alphabet.
- // Note: An NString equivalent does not exist for this method
- // to avoid an ambiguity with the "operator-" function
- // declared in the NString class, which means: remove
- // initial occurrences of an alphabet from an NString.
-
-
- /*-------- Testing the membership of one or more letters --------*/
-
- int contains (const char element) const; // if (a.contains('f')) ...
- // Return 1 iff the given element is a member of the alphabet,
- // 0 otherwise.
-
- int contains (const char *elements) const; // if (a.contains("Hello")) ...
- int contains (const NString& elements) const; // if (a.contains(s)) ...
- // Return 1 iff the alphabet contains ALL OF the given string's letters
-
- /*-------- Logical operations that affect the whole alphabet --------*/
-
- Alphabet& operator+= (const Alphabet& other); // a += b;
- // Perform a logical OR between this alphabet and the other one,
- // then store the result in this alphabet.
-
- Alphabet& operator*= (const Alphabet& other); // a *= b;
- // Perform a logical AND between this alphabet and the other one,
- // then store the result in this alphabet.
-
- Alphabet& operator-= (const Alphabet& other); // a -= b;
- // Store the logical DIFFERENCE between this alphabet and the
- // other one in this alphabet.
-
- inline Alphabet operator+ (const Alphabet& other) const; // a = b + c;
- // Perform a logical OR between this alphabet and the other one,
- // then return the result.
-
- inline Alphabet operator* (const Alphabet& other) const; // a = b * c;
- // Perform a logical AND between this alphabet and the other one,
- // then return the result.
-
- inline Alphabet operator- (const Alphabet& other) const; // a = b - c;
- // Return the logical DIFFERENCE between this alphabet and the other one.
-
-
- inline Alphabet& operator*= (const char *string); // a *= "Hello";
- inline Alphabet& operator*= (const NString& string); // a *= s;
- // Perform a logical AND between this alphabet and the one
- // defined by the letters of the string, then store the result in this alphabet.
-
- inline Alphabet operator* (const char *string) const; // a = b * "Hello";
- inline Alphabet operator* (const NString& string) const; // a = b * s;
- // Perform a logical AND between this alphabet and the one
- // defined by the letters of the string, then return the result.
-
-
- Alphabet& complement (void); // a.complement();
- // Transform this alphabet into its complement.
-
- inline Alphabet operator- (void) const; // b = -a;
- // Return this alphabet's complement.
-
-
- /*-------- Iterator --------*/
-
- enum direction_t {FORWARD, BACKWARD};
-
- Alphabet& through (int (*action)(const char, Alphabet&), direction_t dir = FORWARD);
- // Traverse the alphabet in the specified directon, calling at each step the given function.
- // The "action" function receives as parameters the current character
- // and a reference to the alphabet being traversed.
- // If the alphabet is empty, the iterator does nothing.
- // If the alphabet is altered during iteration, these modifications will NOT affect the iteration
- // process, as the alphabet being traversed is actually a copy of the original alphabet.
- // The "action" may interrupt the iteration process by returning a non-zero value.
- // Zero must be returned for the iteration to proceed until the end of the alphabet is reached.
- // The iterator returns a reference to the alphabet being traversed.
-
- // Example: "a.through(&print, Alphabet::FORWARD)" where print() has been previously defined;
-
-
- /*-------- Getting the number of alphabet elements --------*/
-
- unsigned int card (void) const; // n = a.card();
- // Return the number of elements in this alphabet.
-
-
- /*-------- Comparisons --------*/
-
- // The function variants that take a string or an NString argument
- // construct a temporary alphabet from the string's letters to use as
- // "other alphabet".
-
- int operator== (const Alphabet& other) const; // if (a == b) ...
- int operator== (const char *other) const; // if (a == "*-+/") ...
- int operator== (const NString& other) const; // if (a == s) ...
- // Return 1 if this alphabet is equal to the other one, 0 otherwise.
-
- inline int operator!= (const Alphabet& other) const; // if (a != b) ...
- inline int operator!= (const char *other) const; // if (a != "ABC") ...
- inline int operator!= (const NString& other) const; // if (a != s) ...
- // Return 1 if this alphabet is not equal to the other one, 0 otherwise.
-
- inline int operator<= (const Alphabet& other) const; // if (a <= b) ...
- inline int operator<= (const char *other) const; // if (a <= "0123456789") ...
- inline int operator<= (const NString& other) const; // if (a <= s) ...
- // Return 1 if this alphabet is contained in
- // or equal to the other one, 0 otherwise.
-
- inline int operator< (const Alphabet& other) const; // if (a < b) ...
- inline int operator< (const char *other) const; // if (a < "1234") ...
- inline int operator< (const NString& other) const; // if (a < s) ...
- // Return 1 if this alphabet is contained in,
- // but not equal to the other one, 0 otherwise.
- };
-
- #include "Alphabet_Inlines.h"
-
- #ifndef KEEP_ALPHABET_DEFINES
- #undef BUFSIZE
- #undef ELT_HIGH
- #undef ELT_LOW
- #endif
-
- #endif
-